<template>
  <el-main>
    <h1 style="margin-bottom: 20px">{{ this.id ? '编辑英雄' : '新建英雄'}}</h1>
      <el-form ref="form" :model="form" label-width="80px">
        <el-tabs type="border-card" value="skills">
          <el-tab-pane label="基础信息">
            <el-form-item label="名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="称号">
              <el-input v-model="form.title"></el-input>
            </el-form-item>

            <el-form-item label="头像">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                >
                  <img v-if="form.avatar" :src="form.avatar" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item label="Banner">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="res => $set(this.form, 'banner', res.url)"
                >
                  <img v-if="form.banner" :src="form.banner" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item label="类型">
              <el-select v-model="form.categories" multiple placeholder="请选择">
                <el-option 
                  v-for="option in categoriesOptions"
                  :key="option._id"
                  :label="option.name" 
                  :value="option._id"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="难度">
              <el-rate style="margin-top: 12px" :max="9" show-score v-model="form.scores.difficult"></el-rate>
            </el-form-item>
            
            <el-form-item label="技能">
              <el-rate style="margin-top: 12px" :max="9" show-score v-model="form.scores.skill"></el-rate>
            </el-form-item>

            <el-form-item label="攻击">
              <el-rate style="margin-top: 12px" :max="9" show-score v-model="form.scores.attack"></el-rate>
            </el-form-item>

            <el-form-item label="生存">
              <el-rate style="margin-top: 12px" :max="9" show-score v-model="form.scores.survive"></el-rate>
            </el-form-item>

            <el-form-item label="顺风出装">
              <el-select v-model="form.item1" multiple placeholder="请选择">
                <el-option 
                  v-for="option in itemsOptions"
                  :key="option._id"
                  :label="option.name" 
                  :value="option._id"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="逆风出装">
              <el-select v-model="form.item2" multiple placeholder="请选择">
                <el-option 
                  v-for="option in itemsOptions"
                  :key="option._id"
                  :label="option.name" 
                  :value="option._id"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="使用技巧">
              <el-input type="textarea" v-model="form.usageTips"></el-input>
            </el-form-item>

            <el-form-item label="对抗技巧">
              <el-input type="textarea" v-model="form.battleTips"></el-input>
            </el-form-item>

            <el-form-item label="团战思路">
              <el-input type="textarea" v-model="form.teamTips"></el-input>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="技能" name="skills">
            <el-button size="small" @click="form.skills.push({})"><i class="el-icon-plus"></i>添加技能</el-button>
            <el-row type="flex" style="flex-wrap: wrap;margin-top: 15px">
              <el-col :md="12" v-for="(item, i) in form.skills" :key="i">
                <el-form-item label="名称">
                  <el-input v-model="item.name"></el-input>
                </el-form-item>
                <el-form-item label="图标">
                    <el-upload
                      class="avatar-uploader"
                      :action="uploadUrl"
                      :headers="getAuthHeaders()"
                      :show-file-list="false"
                      :on-success="res => $set(item, 'icon', res.url)"
                    >
                      <img v-if="item.icon" :src="item.icon" class="avatar">
                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="描述">
                  <el-input type="textarea" v-model="item.description"></el-input>
                </el-form-item>
                <el-form-item label="小提示">
                  <el-input type="textarea" v-model="item.tips"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button size="small" type="danger" @click="form.skills.splice(i, 1)">删除</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="搭档" name="partners">
            <el-button size="small" @click="form.partners.push({})"><i class="el-icon-plus"></i>添加英雄</el-button>
            <el-row type="flex" style="flex-wrap: wrap;margin-top: 15px">
              <el-col :md="12" v-for="(item, i) in form.partners" :key="i">
                <el-form-item label="名称">
                  <el-select filterable v-model="item.hero">
                    <el-option v-for="hero in heroes" :key="hero._id" 
                      :value="hero._id"  
                      :label="hero.name"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="描述">
                  <el-input type="textarea" v-model="item.description"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button size="small" type="danger" @click="form.partners.splice(i, 1)">删除</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        
        <el-form-item style="margin-top: 20px">
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-main>
</template>
<script>
  export default {
    props: {
      id: {type: String}
    },
    data() {
      return {
        categoriesOptions: [], // 类型下拉选选项
        itemsOptions: [], // 装备下拉选选项
        heroes: [],
        form: {
          name: '',
          avatar: '',
          scores: {
            difficult: 0,
            skill: 0,
            attack: 0,
            survive: 0,
          },
          item1: '',
          item2: '',
          skills: [],
          partners: []
        },
      }
    },
    created() {
      this.fetchCategories()
      this.fetchItems()
      this.fetchHeroes()
      this.id && this.fetch()
    },
    methods: {
      handleAvatarSuccess(res) {
        this.$set(this.form, 'avatar', res.url)
      },
      async onSubmit() {
        let res
        if(this.id) {
          res = await this.$http.put(`curd/hero/${this.id}`, this.form)
        } else {
          res = await this.$http.post('curd/hero', this.form)
        }
        if(res.data.code === 200) {
          this.$message({
            message: '保存成功！',
            type: 'success'
          });
          this.$router.push('/hero/list')
        }
      },
      async fetch() {
        const res = await this.$http.get(`curd/hero/${this.id}`)
        this.form = Object.assign({}, this.form, res.data) 
      },
      async fetchCategories() {
        const res = await this.$http.get(`curd/category`)
        this.categoriesOptions = res.data
      },
      async fetchItems() {
        const res = await this.$http.get('curd/item')
        this.itemsOptions = res.data
      },
      async fetchHeroes() {
        const res = await this.$http.get('curd/heroes')
        this.heroes = res.data
      },
    }
  }
</script>
